package com.smyh.UIM.host.param;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.ScrollView;

import com.smyh.UIM.R;
import com.smyh.cache.UIM.host.Host101PointState;
import com.smyh.model.host.param.Host101PointModel;

public class Host101PointFm extends Fragment {
    private RadioButton pointType01,pointType02,pointType03,pointType04;
    private Button localPointTbtn,hostPointTbtn,alertSendBtn;
    private ScrollView scrollView;
    private static Handler handler;

    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_host_101point, container, false);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(scrollView != null){
                    switch (msg.what){
                        case 1:{
                            scrollView.removeAllViews();
                            scrollView.addView(Host101PointModel.getDigitTableLayout(getContext()));
                            Host101PointModel.setTableData(Host101PointState.pointSource , 1);
                            break;
                        }
                        case 2:{
                            scrollView.removeAllViews();
                            scrollView.addView(Host101PointModel.getMockTableLayout(getContext()));
                            Host101PointModel.setTableData(Host101PointState.pointSource ,2);
                            break;
                        }
                        case 3:{
                            scrollView.removeAllViews();
                            scrollView.addView(Host101PointModel.getSetTableLayout(getContext()));
                            Host101PointModel.setTableData(Host101PointState.pointSource ,3);
                            break;
                        }
                        case 4:{
                            scrollView.removeAllViews();
                            scrollView.addView(Host101PointModel.getControlTableLayout(getContext()));
                            Host101PointModel.setTableData(Host101PointState.pointSource ,4);
                            break;
                        }
                    }
                }
            }
        };

        initView();
        initEvent();
        return view;
    }

    private void initEvent() {
        pointType01.setOnCheckedChangeListener(rrll);
        pointType02.setOnCheckedChangeListener(rrll);
        pointType04.setOnCheckedChangeListener(rrll);
        pointType03.setOnCheckedChangeListener(rrll);

        localPointTbtn.setOnClickListener(btnClickListener);
        hostPointTbtn.setOnClickListener(btnClickListener);
        alertSendBtn.setOnClickListener(btnClickListener);
    }

    private void initView() {
        pointType01 = (RadioButton) view.findViewById(R.id.pointType01);
        pointType02 = (RadioButton) view.findViewById(R.id.pointType02);
        pointType03 = (RadioButton) view.findViewById(R.id.pointType03);
        pointType04 = (RadioButton) view.findViewById(R.id.pointType04);

        localPointTbtn = (Button) view.findViewById(R.id.localPointTbtn);
        localPointTbtn.setBackgroundColor(getResources().getColor(R.color.blackGray));
        localPointTbtn.setText("本地点号");

        hostPointTbtn = (Button) view.findViewById(R.id.hostPointTbtn);
        hostPointTbtn.setBackgroundColor(getResources().getColor(R.color.lightGreen));
        hostPointTbtn.setText("主机点号");

        alertSendBtn = (Button) view.findViewById(R.id.alertSendBtn);
        alertSendBtn.setBackgroundColor(getResources().getColor(R.color.lightGreen));

        scrollView = (ScrollView) view.findViewById(R.id.scrollView7);
//        scrollView.addView(Host101PointModel.getDigitTableLayout(getContext()));
//        Host101PointModel.setTableLayout(1);
        freshPointSetUI(1);
    }

    private CompoundButton.OnCheckedChangeListener rrll = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(isChecked){
                switch (buttonView.getId()){
                    case R.id.pointType01:{
                        Host101PointState.pointChooseType = 1;
                        freshPointSetUI(1);
                        break;
                    }
                    case R.id.pointType02:{
                        Host101PointState.pointChooseType = 2;
                        freshPointSetUI(2);
                        break;
                    }
                    case R.id.pointType03:{
                        Host101PointState.pointChooseType = 3;
                        freshPointSetUI(3);
                        break;
                    }
                    case R.id.pointType04:{
                        Host101PointState.pointChooseType = 4;
                        freshPointSetUI(4);
                        break;
                    }
                    default:
                        break;
                }
            }
        }
    };

    public View.OnClickListener btnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int clickId = v.getId();
            switch (clickId){
                case R.id.localPointTbtn:{
                    Host101PointState.pointSource = 1;
                    localPointTbtn.setBackgroundColor(getResources().getColor(R.color.lightGreen));
                    hostPointTbtn.setBackgroundColor(getResources().getColor(R.color.blackGray));
                    localPointTbtn.setText("本地点号");
                    hostPointTbtn.setText("主机点号");
                    freshPointSetUI(Host101PointState.pointChooseType);
                    break;
                }
                case R.id.hostPointTbtn:{
                    Host101PointState.pointSource = 2;
                    hostPointTbtn.setBackgroundColor(getResources().getColor(R.color.lightGreen));
                    localPointTbtn.setBackgroundColor(getResources().getColor(R.color.blackGray));
                    localPointTbtn.setText("本地点号");
                    hostPointTbtn.setText("主机点号");
                    freshPointSetUI(Host101PointState.pointChooseType);
                    break;
                }
                case R.id.alertSendBtn:{
                    break;
                }
            }

        }
    };

    /**
     * 刷新101点位数据界面：
     * @param type-1:遥信；2-遥测；3-遥设置；4-遥控
     */
    public static void freshPointSetUI(int type){
        Message message = new Message();
        message.what = type;
        handler.sendMessage(message);
    }
}
